package com.ece.control;

import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.ece.CommandeBusinessControllerRemote;
import com.ece.beans.items.Item;
import com.ece.gui.KiwiMainFrame;
import com.ece.items.ItemBusinessControllerRemote;
import com.ece.utils.ContextUtil;

public class ItemController {

	public static void addItemToCart() {
		KiwiMainFrame frame = (KiwiMainFrame)GeneralController.getInstance().getContainer(KiwiMainFrame.class.getName());

		frame.getCart().addItemList(frame.getItemList().getItemListSelected());
	}

	public static void RemoveItemFromCart() {
		KiwiMainFrame frame = (KiwiMainFrame)GeneralController.getInstance().getContainer(KiwiMainFrame.class.getName());

		frame.getCart().RemoveItemFromCart(frame.getCart().getItemListSelected());
	}

	public static void setItemList(String categ) {
		List<Item> itemList;
		try {
			Context context = new InitialContext(ContextUtil.getInstance().getEnv());
			ItemBusinessControllerRemote ibc = (ItemBusinessControllerRemote)context.lookup("ItemBusinessController/remote");

			itemList = new ArrayList<Item>();
			if("Livres".equals(categ))
				//				itemList.add(new Livre("Livre 2", 6f, "God Hates Us All2", "Hank Moody"));
				//				itemList.add(new Livre("Livre 3", 6f, "God Hates Us All3", "Hank Moody"));
				//				itemList.add(new Livre("Livre 4", 6f, "God Hates Us All4", "Hank Moody"));
				//				itemList.add(new Livre("Livre 5", 6f, "God Hates Us All5", "Hank Moody"));
				itemList = ibc.getAllLivres();
			else if("DVD".equals(categ))
				itemList = ibc.getAllDvd();
			//				itemList.add(new DVD("DVD 1", 9.99f, "La Fureur du Dragon", 2));
			//				itemList.add(new DVD("DVD 2", 9.99f, "La Fureur du Dragon2", 2));
			//				itemList.add(new DVD("DVD 3", 9.99f, "La Fureur du Dragon3", 2));
			//				itemList.add(new DVD("DVD 4", 9.99f, "La Fureur du Dragon4", 2));


			KiwiMainFrame frame = (KiwiMainFrame)GeneralController.getInstance().getContainer(KiwiMainFrame.class.getName());
			frame.getItemList().setItemList(itemList);

		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	public static void sendCommande() {
		KiwiMainFrame frame = (KiwiMainFrame)GeneralController.getInstance().getContainer(KiwiMainFrame.class.getName());

		try {
			Context context = new InitialContext(ContextUtil.getInstance().getEnv());
			CommandeBusinessControllerRemote cbc = (CommandeBusinessControllerRemote)context.lookup("CommandeBusinessController/remote");

			cbc.validerCommande(frame.getCart().getCart(), frame.getUtilisateur());

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
